﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using System.Configuration;
using Enter.DataCommon;
using Enter.DataProxy.DbConnector;

namespace Enter.DataProxy.Configuration
{
    /// <summary>
    /// 链接字符串组的配置管理
    /// </summary>
    /// <remarks>
    /// 从默认的企业库获得(Web.config/*.exe.config),如果为企业库配置了独立的文件，则跟着配置在独立的文件中。
    /// </remarks>
    public class ConnectionsConfigManager : ConfigurationSection
    {
       // private static readonly ObjectsContainer _Container;
        //private static readonly string _ConnectionRegion = "Vancl.VStar.DataProxy.DbConnector.Configuration.Connections";

        //private static readonly ConfigurationPropertyCollection _Properties;
        //private static readonly ConfigurationProperty _ConnectionProperty;

        ///// <summary>
        ///// 一组数据库链接字符串
        ///// </summary>
        //public ConnectionNameElementCollection ConnectionNames
        //{
        //    get { return (ConnectionNameElementCollection)this[_ConnectionProperty]; }
        //    set { this[_ConnectionProperty] = value; }
        //}

        //protected override ConfigurationPropertyCollection Properties
        //{
        //    get
        //    {
        //        return _Properties;
        //    }
        //}

        #region .ctor

        //static ConnectionsConfigManager()
        //{
        //    _Container = ObjectsContainer.Default;
        //    _Properties = new ConfigurationPropertyCollection();
        //    _ConnectionProperty = new ConfigurationProperty("connections", typeof(ConnectionNameElementCollection), null, ConfigurationPropertyOptions.IsRequired);
        //    _Properties.Add(_ConnectionProperty);
        //}

        public ConnectionsConfigManager()
        {
        }

        #endregion

        ///// <summary>
        ///// 配置文件中获取“链接字符串组”
        ///// </summary>
        ///// <param name="name">链接字符串组的Section名称或链接字符串的名称</param>
        ///// <returns></returns>
        ///// <remarks>注：Pools&lt;string&gt;中存放的是连接字符串的名称，并非链接字符串本身。连接字符串本身依然放在configuration/connections中。</remarks>
        //public static Pools<string> ConnectionGroup(string name)
        //{
        //    return _Container.Get<string, Pools<string>>(_ConnectionRegion, name, () =>
        //    {
        //        Pools<string> myPools;
        //        // 获取企业库配置的config文件
        //        IConfigurationSource source = ConfigurationSourceFactory.Create();
        //        ConnectionsConfigManager section = (ConnectionsConfigManager)source.GetSection(name);
        //        // 如果section没有配置，则将name当作链接字符串的名称来处理。
        //        if (section == null)
        //        {
        //            // 查找connectionStrings
        //            ConnectionStringsSection connectionStrings = (ConnectionStringsSection)source.GetSection("connectionStrings");
        //            string connectionString = connectionStrings.ConnectionStrings[name].ConnectionString;
        //            if (string.IsNullOrWhiteSpace(connectionString))
        //                throw new ArgumentException(string.Format("[{0}]在配置文件中无法找到。", name));
        //            // 如果存在则Pools对象中就放一个，因为参数name就是链接字符串的名称，而非链接字符串组
        //            myPools = new Pools<string>(new List<string>() { name });
        //        }
        //        else
        //        {
        //            List<string> list = (from ConnectionNameElement element in section.ConnectionNames select element.ConnectionName).ToList();
        //            myPools = new Pools<string>(list);
        //        }
        //        return myPools;
        //    });
        //}

        /// <summary>
        /// 获取数据库链接字符串
        /// </summary>
        /// <param name="connName"></param>
        /// <returns></returns>
        public static ConnectionStringSettings ConnectionSettings(string connName)
        {
            // 获取企业库配置的config文件
            IConfigurationSource source = ConfigurationSourceFactory.Create();
            // 查找connectionStrings
            ConnectionStringsSection connectionStrings = (ConnectionStringsSection)source.GetSection("connectionStrings");
            return connectionStrings.ConnectionStrings[connName];
        }
    }
}
